﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iss.sg4210.se18.team6s.jass.DAL.DataAccess.Interfaces;
using iss.sg4210.se18.team6s.jass.DAL.DataAccess.MSSQL;
using iss.sg4210.se18.team6s.jass.DAL;
using System.Transactions;

namespace iss.sg4210.se18.team6s.jass.BL
{
    public class PublishingsFacade
    {
        IEvents eventsRepo = null;
        IAddresses addressRepo = null;
        IArticles articlesRepo = null;

        public PublishingsFacade()
        {
            eventsRepo = new EventsDAO();
            articlesRepo = new ArticleDAO();
            addressRepo = new AddressesDAO();
        }

        #region Events
        public void AddEvent(Event eventObj, Address address)
        {
            using (TransactionScope ts = new TransactionScope())
            {

                //AddAddress(address);
                //get address id and set to eventsObj
                eventObj.Address = address;
                eventsRepo.AddEvent(eventObj);
                ts.Complete();
            }
        }

        public IQueryable<Event> FindAllEventsByUser(Guid userId)
        {
            return eventsRepo.FindAllEventsByUser(userId);
        }

        public Event GetEvent(int id)
        {
            return eventsRepo.GetEvent(id);
        }

        public void UpdateEvent(Event events, Address address)
        {
            using (TransactionScope ts = new TransactionScope())
            {
                UpdateAddress(address);
                eventsRepo.UpdateEvent(events);

                ts.Complete();
            }
        }

        public void DeleteEvent(int id)
        {
            using (TransactionScope ts = new TransactionScope())
            {
                Event eve = GetEvent(id);
                eventsRepo.DeleteEvent(id);
                DeleteAddress(eve.AddressId);

                ts.Complete();
            }
        }
        #endregion

        #region Article
        public IQueryable<Article> FindAllArticlesByUser(Guid userId)
        {
            return articlesRepo.FindAllArticlesByUser(userId);
        }

        public Article GetArticle(int id)
        {
            return articlesRepo.GetArticle(id);
        }

        public void AddArticle(Article article)
        {
            articlesRepo.AddArticle(article);
        }

        public void UpdateArticle(Article article)
        {
            articlesRepo.UpdateArticle(article);
        }

        public void DeleteArticle(int id)
        {
            articlesRepo.DeleteArticle(id);
        }
        #endregion

        #region Address
        public IQueryable<Address> FindAllAddresses()
        {
            return addressRepo.FindAllAddresses();
        }

        public Address GetAddress(int id)
        {
            return addressRepo.GetAddress(id);
        }

        public void AddAddress(Address address)
        {
            addressRepo.AddAddress(address);
        }

        public void UpdateAddress(Address address)
        {
            addressRepo.UpdateAddress(address);
        }

        public void DeleteAddress(int id)
        {
            addressRepo.DeleteAddress(id);
        }
        #endregion
    }
}
